;/*==================================================================================================
;  Description   Main
;  
;  20080905      Wim       Created
;==================================================================================================*/
#include general.ahk
#include CommandLine.ahk

TestScriptName := "JvcDomeTestMouseScript - 1.ahk"
                 

;   # win key
;   ! alt
;   ^ ctrl
;   + shift
;   $RButton $#RButton $!RButton $+RButton $+! etc
;   $LButton

SetMouseDelay, 75
HotKey, ^!+1, Start
HotKey, ^!+2, Stop
HotKey, ^!+3, RunOnce
HotKey, ^!+q, Quit
HotKey, ^!+x, Quit
HotKey, ^!+r, DoReload
return


;/*==================================================================================================
;  Description
;  
;  20080905      Wim       Created
;==================================================================================================*/
Quit:
  ExitApp
  return


;/*==================================================================================================
;  Description
;  
;  20090609      Wim       Created
;==================================================================================================*/
RunOnce:
  Runs = 1
  goto, DoRun
  return


;/*==================================================================================================
;  Description
;  
;  20090609      Wim       Created
;==================================================================================================*/
Start:
  Runs = 0
  goto, DoRun
  return
  
  


;/*==================================================================================================
;  Description
;  
;  20080905      Wim       Created
;==================================================================================================*/
DoRun:
  osd := "Ctrl-Alt-Shift 1:run, 2:stop, 3:run once q:quit, r:reload."
  delta := 2
  sleeper := 0
  outX := 0
  outY := 0
  state := 0
  running := 1
  index := 1
  lines := ""
  lineCount := 0
  line := ""
  file := TestScriptName
  params =
  
  
  SplashTextOn, 400, 110, Diva Script, --ods--
  WinMove, Diva Script, , 400  , 0
  
  Loop, Read, %file%
  {
    lineCount += 1
    lines%lineCount% := A_LoopReadLine
  }
  
  Loop
  {
    loop %lineCount%
    {
      if (Execute(A_Index , lines%A_Index%, osd, delta))
        running := 0
      
      if (running = 0)
        break
    }
    if (running = 0)
      break
      
    if (Runs = 1)
      break
  }
  SplashTextOff
  return


;/*==================================================================================================
;  Description
;  
;  20080905      Wim       Created
;==================================================================================================*/
Stop:
  running := 0
  return



DoReload:
  Reload
  Sleep 1000 ; If successful, the reload will close this instance during the Sleep, so the line below will never be reached.
;  MsgBox, 4,, The script could not be reloaded. Would you like to open it for editing?
;  IfMsgBox, Yes, Edit
  return



Execute(index, line, osd, delta)
{
  inf = %index%: %line%
  SetControlText(osd, inf) 
  StringSplit, params, line, `,
  params1 = %params1%
  params2 = %params2%
  params3 = %params3%
  params4 = %params4%
  params5 = %params5%
  ;MsgBox %index% : [%params1%]`, [%params2%]`, [%params3%]`, [%params4%]
  
  if (params1 = "")
  {
    ;empty line
  }
  else if (params1 = "WinWait")
  {
    WinWait, %params2%,
  }
  else if (params1 = "IfWinNotActive")
  {
    IfWinNotActive, %params2%, %params3% 
    {
      WinActivate, %params5%, params6
    }
  }
  else if (params1 = "WinWaitActive")
  {
    WinWaitActive, %params2%, %params3%  
  }
  else if (params1 = "MouseClick")
  {
    MouseClickRandomPath(params3, params4, delta) 
  }
  else if (params1 = "MouseClickRel")
  {
    MouseClickRel(params3, params4)
  }
  else if (params1 = "Send")
  {
    Send, %params2%
  }
  else if (params1 = "Sleep")
  {
    Sleep, %params2%
  }
  else if (params1 = "SetTitleMatchMode")
  {
    SetTitleMatchMode, %params2%
  }
  else if (params1 = "Stop")
  {
    return true
  }
  else
  {
    MsgBox Unhandled command: [%params1%]
  }
  return false
}

SetControlText(osd, line)
{
  static line2 
  static line1 
  static line0 

  line2 := line1
  line1 := line0
  line0 := line
  
  ControlSetText, Static1, %osd%`n%line2%`n%line1%`n%line0%, Diva Script
}
